﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace DRSM
{
    class SupportPersonnelControl
    {
        public static void AddPersonnel(int count, TextBox tbxNameVerify, TextBox tbxPersonnelIDVerify, TextBox tbxYearsOfExperience,
            TextBox tbxServicesProvide, ComboBox cbxTypeOfPersonnel, ComboBox cbxID)
        {
            count++;
            string nameOfPersonnel = tbxNameVerify.Text.Trim();
            string personnelID = cbxID.Text + tbxPersonnelIDVerify.Text.Trim();
            string yearsOfExperience = tbxYearsOfExperience.Text.Trim();
            string serviceProvide = tbxServicesProvide.Text.Trim();
            string typeOfPersonnel = cbxTypeOfPersonnel.Text;
            string cityAssigned = "NILL";


            int num = 1000000;
            num = num + count;


            bool empty = false;
            bool icInvalid = false;

            if (empty == false && icInvalid == false)
            {
                string icNum = (cbxID.Text + tbxPersonnelIDVerify.Text);
                //SupportPersonnels s = new SupportPersonnels(num, tbxNameVerify.Text, icNum, tbxYearsOfExperience.Text, tbxServicesProvide.Text, typeOfPersonnel, cityAssigned);
                //AllFormClass.SupportPersonnels.Add(s);
                dbmanager.AddPersonnel(num, nameOfPersonnel, personnelID, yearsOfExperience, serviceProvide, typeOfPersonnel);

                MessageBox.Show("Personnel Added Successfully!! ");
                dbmanager.GetAllPersonnel();
            }
            tbxNameVerify.Clear();
            tbxPersonnelIDVerify.Clear();
            tbxYearsOfExperience.Clear();
            tbxServicesProvide.Clear();
            cbxID.SelectedIndex = 0;
            cbxTypeOfPersonnel.SelectedIndex = 0;
        }


        //public static void UpdateSearch(ListBox lbxSearchResult, ComboBox cbxUpdateSearch, TextBox tbxUpdateSearch)
        //{
        //    lbxSearchResult.Items.Clear();

        //    if (cbxUpdateSearch.SelectedItem.Equals("Support Personnel Name"))
        //    {
        //        for (int i = 0; i < AllFormClass.SupportPersonnels.Count; i++)
        //        {
        //            SupportPersonnels s = (SupportPersonnels)AllFormClass.SupportPersonnels[i];
        //            string searchPersonnel = s.NameOfPersonnel;
        //            if (searchPersonnel.Contains(tbxUpdateSearch.Text))
        //            {
        //                lbxSearchResult.DisplayMember = "NameOfPersonnel";
        //                lbxSearchResult.Items.Add(AllFormClass.SupportPersonnels[i]);
        //            }

        //        }
        //    }
        //    else if (cbxUpdateSearch.SelectedItem.Equals("Personnel ID"))
        //    {
        //        for (int i = 0; i < AllFormClass.SupportPersonnels.Count; i++)
        //        {
        //            SupportPersonnels s = (SupportPersonnels)AllFormClass.SupportPersonnels[i];
        //            string searchPersonnel = s.PersonnelIC.ToString();
        //            if (searchPersonnel.Contains(tbxUpdateSearch.Text))
        //            {
        //                lbxSearchResult.DisplayMember = "personnelIC";
        //                lbxSearchResult.Items.Add(AllFormClass.SupportPersonnels[i]);
        //            }
        //        }
        //    }
        //}
        //public static void SearchUpdatePersonnel(ListBox lbxSearchResult, TextBox tbxUpdateNameOfPersonnel, ComboBox cbxUpdateID,
        //    TextBox tbxUpdatePersonnelNo, TextBox tbxUpdateID, TextBox tbxUpdateYearsOfExperience, TextBox tbxUpdateServicesProvided,
        //    ComboBox cbxUpdateTypeOfPersonnel, Panel pnlUpdatePersonnelDetails)
        //{
        //    foreach (SupportPersonnels SupportPersonnels in AllFormClass.SupportPersonnels)
        //    {
        //        SupportPersonnels s1 = (SupportPersonnels)lbxSearchResult.SelectedItem;

        //        tbxUpdateNameOfPersonnel.Text = s1.NameOfPersonnel;
        //        char idType = s1.PersonnelIC[0];
        //        if (idType.Equals('S'))
        //        {
        //            cbxUpdateID.SelectedIndex = 0;
        //        }
        //        else if (idType.Equals('F'))
        //        {
        //            cbxUpdateID.SelectedIndex = 1;
        //        }
        //        else if (idType.Equals('T'))
        //        {
        //            cbxUpdateID.SelectedIndex = 2;
        //        }
        //        else if (idType.Equals('G'))
        //        {
        //            cbxUpdateID.SelectedIndex = 3;
        //        }
        //        //tbxUpdatePersonnelNo.Text = Convert.ToString(SupportPersonnels.personnelID);
        //        tbxUpdateID.Text = s1.PersonnelIC.Remove(0, 1);
        //        tbxUpdateYearsOfExperience.Text = s1.YearsOfExperience;
        //        tbxUpdateServicesProvided.Text = s1.ServiceProvided;

        //        char typeofPersonnel = s1.TypeOfPersonnel[0];
        //        if (s1.TypeOfPersonnel == ("Medical"))
        //        {
        //            cbxUpdateTypeOfPersonnel.SelectedIndex = 0;
        //        }
        //        else if (typeofPersonnel.Equals("Military"))
        //        {
        //            cbxUpdateTypeOfPersonnel.SelectedIndex = 1;
        //        }
        //        else if (typeofPersonnel.Equals("Volunteer"))
        //        {
        //            cbxUpdateTypeOfPersonnel.SelectedIndex = 2;
        //        }

        //        tbxUpdatePersonnelNo.Text = s1.personnelID.ToString(); //asdasdasd+++++ 
        //        dbmanager.GetAllPersonnel();

        //        pnlUpdatePersonnelDetails.Show();
        //    }
        //}
        public static void UpdatePersonnel(TextBox tbxUpdatePersonnelNo, ComboBox cbxUpdateID, TextBox tbxUpdateID,
            TextBox tbxUpdateNameOfPersonnel, TextBox tbxUpdateServicesProvided, TextBox tbxUpdateYearsOfExperience,
            ComboBox cbxUpdateTypeOfPersonnel)
        {
            foreach (SupportPersonnels SupportPersonnels in AllFormClass.SupportPersonnels)
            {
                if (Convert.ToString(SupportPersonnels.personnelID) == tbxUpdatePersonnelNo.Text)
                {
                    string icNum = (cbxUpdateID.Text + tbxUpdateID.Text);
                    SupportPersonnels.personnelID = Convert.ToInt32(tbxUpdatePersonnelNo.Text);
                    SupportPersonnels.NameOfPersonnel = tbxUpdateNameOfPersonnel.Text;
                    SupportPersonnels.PersonnelIC = icNum;
                    SupportPersonnels.ServiceProvided = tbxUpdateServicesProvided.Text;
                    SupportPersonnels.YearsOfExperience = tbxUpdateYearsOfExperience.Text;
                    SupportPersonnels.TypeOfPersonnel = cbxUpdateTypeOfPersonnel.Text;
                }
                dbmanager.UpdatePersonnel(SupportPersonnels.personnelID, SupportPersonnels.NameOfPersonnel, SupportPersonnels.PersonnelIC, SupportPersonnels.YearsOfExperience, SupportPersonnels.ServiceProvided, SupportPersonnels.TypeOfPersonnel);
            }
        }

        //public static void SearchDelete(Panel pnlDeletePersonnelDetails, ListBox searchDeletePersonnelResult,
        //    Label lblDeleteNameOfPersonnel, Label lblDeletePersonnelIC, Label lblDeleteYearsOfExperience, Label lblDeleteServicesProvided,
        //    Label lblDeleteTypeOfPersonnel)
        //{
        //    pnlDeletePersonnelDetails.Show();
        //    string selectedPersonnel = searchDeletePersonnelResult.SelectedItem.ToString();
        //    string selectedIc = searchDeletePersonnelResult.SelectedItem.ToString();
        //    foreach (SupportPersonnels SupportPersonnels in AllFormClass.SupportPersonnels)
        //    {
        //        if (SupportPersonnels.NameOfPersonnel == selectedPersonnel)
        //        {
        //            lblDeleteNameOfPersonnel.Text = SupportPersonnels.NameOfPersonnel;
        //            lblDeletePersonnelIC.Text = SupportPersonnels.PersonnelIC;
        //            lblDeleteYearsOfExperience.Text = SupportPersonnels.YearsOfExperience;
        //            lblDeleteServicesProvided.Text = SupportPersonnels.ServiceProvided;
        //            lblDeleteTypeOfPersonnel.Text = SupportPersonnels.TypeOfPersonnel;
        //        }

        //        else if (SupportPersonnels.PersonnelIC == selectedIc)
        //        {
        //            lblDeleteNameOfPersonnel.Text = SupportPersonnels.NameOfPersonnel;
        //            lblDeletePersonnelIC.Text = SupportPersonnels.PersonnelIC;
        //            lblDeleteYearsOfExperience.Text = SupportPersonnels.YearsOfExperience;
        //            lblDeleteServicesProvided.Text = SupportPersonnels.ServiceProvided;
        //            lblDeleteTypeOfPersonnel.Text = SupportPersonnels.TypeOfPersonnel;
        //        }
        //        dbmanager.GetAllPersonnel();
        //    }
        //}
        public static void DeletePersonnel(ArrayList al, DataGridView DGVDelPersonnel)
        {
            DataGridViewSelectedRowCollection tmpdgv = DGVDelPersonnel.SelectedRows;


            dbmanager.GetAllPersonnel();


            //ArrayList integer = new ArrayList();
            //foreach (DataGridViewRow row in DGVDelPersonnel.SelectedRows)
            //{
            //    integer.Add(row.Cells[0].Value);
            //}
            for (int j = 0; j < tmpdgv.Count; j++)
            {
                int todeleteindex = Convert.ToInt32(tmpdgv[j].Cells[5].Value.ToString());
                for (int i = AllFormClass.SupportPersonnels.Count - 1; i >= 0; i--)
                {
                    SupportPersonnels sp = (SupportPersonnels)AllFormClass.SupportPersonnels[i];
                    //for (int j = 0; j < integer.Count; j++)
                    //{
                    //try
                    //{
                    if (sp.personnelID == todeleteindex)
                    {
                        dbmanager.DeleteSupportPersonnel(todeleteindex.ToString());
                    }
                    //}
                    //catch (FormatException fe)
                    //{
                    //    return;
                    //}
                }
            }
            dbmanager.GetAllPersonnel();
            try
            {
                DGVDelPersonnel.DataSource = null;
                DGVDelPersonnel.Refresh();
                if (AllFormClass.SupportPersonnels.Count > 0)
                {
                    DGVDelPersonnel.DataSource = AllFormClass.SupportPersonnels;
                    DGVDelPersonnel.Refresh();
                }
            }
            catch (NullReferenceException nre)
            {
                return;
            }
            //int index = DGVDelPersonnel.CurrentCell.RowIndex;

            //SupportPersonnels sp = (SupportPersonnels)AllFormClass.SupportPersonnels[index];
            //AllFormClass.SupportPersonnels.Remove(sp);

            //DGVDelPersonnel.DataSource = null;
            //DGVDelPersonnel.Refresh();

            //DGVDelPersonnel.DataSource = AllFormClass.SupportPersonnels;
            //DGVDelPersonnel.Refresh();


        }

        public static void ViewPersonnel(DataGridView dataGridViewAllSupportPersonnel)
        {
            dbmanager.GetAllPersonnel();
            /*try
           {
                dataGridViewAllSupportPersonnel.DataSource = null;
                dataGridViewAllSupportPersonnel.Refresh();
                if(AllFormClass.SupportPersonnels.Count >0)
                {
                    dataGridViewAllSupportPersonnel.DataSource = AllFormClass.SupportPersonnels;
                    dataGridViewAllSupportPersonnel.Refresh();
                }
           }
           catch (NullReferenceException nre)
           {
               return;
           }*/
            int rowCount = dataGridViewAllSupportPersonnel.RowCount;
            for (int x = 0; x < rowCount; x++)
            {
                dataGridViewAllSupportPersonnel.Rows.Clear();
            }
            for (int i = 0; i < AllFormClass.SupportPersonnels.Count; i++)
            {
                SupportPersonnels s = (SupportPersonnels)AllFormClass.SupportPersonnels[i];
                dataGridViewAllSupportPersonnel.Rows.Add();
                dataGridViewAllSupportPersonnel.Rows[i].Cells[0].Value = s.NameOfPersonnel;
                dataGridViewAllSupportPersonnel.Rows[i].Cells[1].Value = s.PersonnelIC;
                dataGridViewAllSupportPersonnel.Rows[i].Cells[2].Value = s.YearsOfExperience;
                dataGridViewAllSupportPersonnel.Rows[i].Cells[3].Value = s.TypeOfPersonnel;
                dataGridViewAllSupportPersonnel.Rows[i].Cells[4].Value = s.ServiceProvided;
            }
        }
    }
}



